import 'package:chat/common/routers/index.dart';
import 'package:chat/common/widgets/Avatar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

import '../../../common/widgets/HomeMenu.dart';
import 'index.dart';

class ChatPage extends GetView<ChatController> {
  const ChatPage({super.key});

  // 主视图
  Widget _buildView(ChatController controller) {
    return Obx(() {
      return Container(
        child: SlidableAutoCloseBehavior(
          child: ListView.separated(
            separatorBuilder: (context, index) => Divider(color: Colors.grey.shade300, height: 0.5, thickness: 0.5, indent: 70, endIndent: 0),
            itemCount: controller.chatList.length,
            itemBuilder: (context, index) {
              var chat = controller.chatList[index];
              return Slidable(
                endActionPane: ActionPane(
                  motion: const DrawerMotion(),
                  extentRatio: 0.5,
                  children: [
                    SlidableAction(
                      onPressed: (context) {
                        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("置顶 ${chat.name}")));
                      },
                      backgroundColor: Colors.blue,
                      foregroundColor: Colors.white,
                      icon: Icons.push_pin,
                      label: '置顶',
                    ),
                    SlidableAction(
                      onPressed: (context) {
                        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("删除 ${chat.name}")));
                      },
                      backgroundColor: Colors.red,
                      foregroundColor: Colors.white,
                      icon: Icons.delete,
                      label: '删除',
                    ),
                  ],
                ),
                key: ValueKey(chat.userId),
                child: Container(
                  color: Colors.white,
                  child: ListTile(
                    leading: Avatar(avatar: chat.avatar),
                    title: Text(chat.name),
                    subtitle: Text(chat.lastContent, overflow: TextOverflow.ellipsis, maxLines: 1),
                    onTap:
                        () => Get.toNamed(
                          RouteNames.chatdetail,
                          arguments: {
                            "userId": controller.user.value?.uid,
                            "friendId": chat.userId,
                            "friendName": chat.name,
                            "friendAvatar": chat.avatar,
                          },
                        ),
                  ),
                ),
              );
            },
          ),
        ),
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ChatController>(
      init: ChatController(),
      id: "chat",
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(title: const Text("聊天"), backgroundColor: Colors.white, centerTitle: true, actions: [HomeMenu()]),
          body: SafeArea(child: _buildView(controller)),
        );
      },
    );
  }
}
